package org.xiaom.butler.dbService;

import java.util.ArrayList;
import java.util.List;

import org.xiaom.butler.bean.Department;
import org.xiaom.butler.bean.Hotel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class DepartmentService extends BeanDataService<Department> {

	public DepartmentService(Context context) {
		super(context);
	}

	@Override
	public void insert(List<Department> beans) {

		for (Department bean : beans) {
			ContentValues cv = splitBean(bean);
			this.db.insert(Department.table_name, null, cv);
		}
	}

	@Override
	public void update(List<Department> beans) {
		for (Department bean : beans) {
			ContentValues cv = splitBean(bean);

			this.db.update(Department.table_name, cv, Department.id_ + "="
					+ bean.getId(), null);
		}
	}

	@Override
	public void delete(String[] ids) {
		for (String id : ids) {
			this.db.delete(Department.table_name, Department.id_ + "=" + id,
					null);
		}

	}

	@Override
	public List<Department> get(String[] ids) {
		List<Department> list = new ArrayList<Department>();
		for (String id : ids) {
			Cursor c = db.query(Department.table_name, cols, Department.id_ + "=?", new String[] { id }, null, null, Department.display_index+" asc");
			if (c.getCount() == 1) {
				c.moveToFirst();
				Department bean =parseBean(c);
				list.add(bean);
			}
		}
		return list;
	}

	String[] cols = new String[] { 
			Department.id_,
			Department.ch_name,
			Department.display_index,
			Department.eng_name,
			Department.hotel_id,
			Department.image_path
			};
	public List<Department> getByHotel(String id) {
		 List<Department> list=new ArrayList<Department>();
		 Cursor c = db.query(Department.table_name, cols, Department.hotel_id + "=?", new String[] { id }, null, null, Department.display_index+" asc");
		 while(c.moveToNext()){
			 Department bean=parseBean(c);
			 list.add(bean);
		 }
		return list;
	}

	@Override
	public void deleteAll() {
		db.execSQL("delete from " + Department.table_name);
	}

	@Override
	Department parseBean(Cursor c) {
		Department bean = new Department();
		bean.setId(Integer.parseInt(c.getString(0)));
		bean.setChName(c.getString(1));
		bean.setDisplayIndex(Integer.parseInt(c.getString(2)));
		bean.setEngName(c.getString(3));
		bean.setImagePath(c.getString(5));
		return bean;
	}

	@Override
	ContentValues splitBean(Department bean) {
		ContentValues cv = new ContentValues();

		cv.put(Department.id_, bean.getId());
		cv.put(Department.ch_name, bean.getChName());
		cv.put(Department.display_index, bean.getDisplayIndex().toString());
		cv.put(Department.eng_name, bean.getEngName());
		cv.put(Department.hotel_id, bean.getHotel().getId());
		cv.put(Department.image_path, bean.getImagePath());
		return cv;
	}

}
